﻿using System;
using System.Collections.Generic;
using System.Data.Sql;
using System.Linq;
using System.Web;
using IntraVision.Web;
using IntraVision.Web.Security;
using Model;

/// <summary>
/// Summary description for ScheduleController
/// </summary>
[AllowedRole("IsAdmin")]
public class TaskScheduleController : ControllerBase
{
    private NotarialServiceDataContext dc = new NotarialServiceDataContext();

    private DateTime _Date;

    public void Default()
    {
        List();
    }

    public void List()
    {
        _Date = !string.IsNullOrEmpty(Request["Date"]) ? DateTime.Parse(Request["date"]) : DateTime.Now.AddDays(1);
        LayoutPath = "~/views/layouts/page.haml";
        TemplatePath = "~/views/taskschedule/schedule.haml";
        RegisterStylesheet("taskschedule/list.css");
        RegisterScript("taskschedule/list.js");

        PrepareSchedule();
    }

    public void Print(string date)
    {
        _Date = DateTime.Parse(date);
        LayoutPath = "~/views/layouts/popup.haml";
        TemplatePath = "~/views/taskschedule/_day.haml";
        ViewData["print"] = true;
        RegisterStylesheet("taskschedule/list.css");
        RegisterScript("taskschedule/list.js");

        PrepareSchedule();
    }

    public void Day()
    {
        _Date = DateTime.Parse(Request["date"]);

        LayoutPath = "";
        TemplatePath = "~/views/taskschedule/_day.haml";

        PrepareSchedule();
    }

    private void PrepareSchedule()
    {
        ViewData["StartHour"] = 9;
        ViewData["EndHour"] = 18;
        ViewData["Interval"] = 15;
        ViewData["Date"] = _Date;

        //Запишем в сессию, что мы работали с календарем
        Session["taskmode"] = "taskschedule.ivp?date=" + _Date.ToString("dd.MM.yyyy");

        var schedules = (from s in dc.Schedules orderby s.Id select s).ToList();
        ViewData["Schedules"] = schedules;
        Dictionary<int, IWorkingHours> workingHours = new Dictionary<int, IWorkingHours>(dc.Schedules.Count());
        foreach (var s in schedules)
            workingHours[s.Id] = s.GetWorkingHours(_Date);
        ViewData["WorkingHours"] = workingHours;
        ViewData["Tasks"] = (from t in dc.Tasks where t.StartDate.Date == _Date.Date orderby t.Schedule.Name,t.StartDate select t).ToList();
        
        RenderView();
    }
}
